package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 菜单服务实现
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService{

    @Autowired
    private MenuDao menuDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private JedisPool jedisPool;

    //添加菜单
    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    //分页显示列表
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //        mybatis 分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page = menuDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    //删除
    @Override
    public void del(Integer id) {
        //判断是否与角色关联
        long num = menuDao.findCountByRoleId(id);
        if (num==0){
            throw new RuntimeException("当前菜单被引用，不能删除");
        }
        menuDao.del(id);
    }

    //回显 菜单信息 用于编辑时
    @Override
    public Menu findById(Integer id) {
        Menu menu = menuDao.findById(id);
        return menu;
    }

    //编辑菜单
    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    //查询所有菜单信息
    @Override
    public List<Menu> findAll() {
        List<Menu> menuList = menuDao.findAll();
        return menuList;
    }

    @Override
<<<<<<< HEAD
    public List<Menu> queryAllFirstMenu() {
        List<Menu> firstMenuList = menuDao.queryAllFirstMenu();
        for (Menu firstmenu : firstMenuList) {
            Integer parentId = firstmenu.getId();
            int count = menuDao.hasChildren(parentId);
            if (count!=0) {
                List<Menu> childrenList = menuDao.findChildrenListByParentId(parentId);
                firstmenu.setChildren(childrenList);
            }
        }
        return firstMenuList;
    }

    @Override
    public List<Menu> queryChildrenMenu() {
        List<Menu> childrenMenuList = menuDao.queryChildrenMenu();
        return childrenMenuList;
    }

    @Override
    public List<Menu> findChildrensByParentId(Integer id) {
        List<Menu> childrenList = menuDao.findChildrenListByParentId(id);
        return childrenList;
    }

    @Override
    public List<Menu> findMenuByUsername(String username) {
        List<Integer> roleIdList = menuDao.findRoleIdList(username);
        List<Menu> list = null;
        for (Integer roleId : roleIdList) {
            List<Integer> menuIdList = menuDao.findMenuList(roleId);
            list = new ArrayList<>();
            for (Integer menuId : menuIdList) {
                Menu firstMenu = menuDao.findFirstMenu(menuId);
                if (firstMenu !=null){
                    List<Menu> children = menuDao.findChildrenListByParentId(firstMenu.getId());
                    firstMenu.setChildren(children);
                    list.add(firstMenu);
                }
            }
        }
//        List<Menu> menuList = menuDao.findMenuByUsername(username);
        return list;
=======
    public List<Menu> getUserMenu(String username) {
        String menuRedis = jedisPool.getResource().get(username);
        if (menuRedis==null){
            Integer userId = userDao.findIdByUsername(username);
            Integer roleId = roleDao.findRoleIdByUserId(userId);
            List<Menu> allMenuOne = menuDao.findMenuOneByRoleId(roleId);
            for (Menu menu : allMenuOne) {
                List<Menu> allMenuTwo = menuDao.findMenuTwoByMenuOneId(menu.getId());
                menu.setChildren(allMenuTwo);
            }
            menuRedis = JSON.toJSONString(allMenuOne);
            /*保存到redis*/
            jedisPool.getResource().set(username,menuRedis);
            //System.out.println("从数据库查询");
            return allMenuOne;
        }

        //System.out.println("从redis查询");
        List<Menu> allMenuOne = JSON.parseArray(menuRedis, Menu.class);
        return allMenuOne;


>>>>>>> origin/master
    }

    @Override
    public List<Menu> getUserMenu(String username) {
        String menuRedis = jedisPool.getResource().get(username);
        if (menuRedis==null){
            Integer userId = userDao.findIdByUsername(username);
            Integer roleId = roleDao.findRoleIdByUserId(userId);
            List<Menu> allMenuOne = menuDao.findMenuOneByRoleId(roleId);
            for (Menu menu : allMenuOne) {
                List<Menu> allMenuTwo = menuDao.findMenuTwoByMenuOneId(menu.getId());
                menu.setChildren(allMenuTwo);
            }
            menuRedis = JSON.toJSONString(allMenuOne);
            /*保存到redis*/
            jedisPool.getResource().set(username,menuRedis);
            //System.out.println("从数据库查询");
            return allMenuOne;
        }

        //System.out.println("从redis查询");
        List<Menu> allMenuOne = JSON.parseArray(menuRedis, Menu.class);
        return allMenuOne;


    }
}
